<?php
/**
 * Created by PhpStorm.
 * User: dengsitan
 * Date: 16/8/28
 * Time: 下午10:25
 */

namespace Api\Controller;

use \Think\Controller;


class InVesTiGateController extends Controller{


    /**
     * 获取部门信息和员工信息
     */
    public function getInfo(){
        $staff_num = I('staff_num/d');
        $staffInfo = array();
        if($staff_num){
           $staffInfo = $this->checkUserExist($staff_num);
           $staffDepartment = M('department')->where("id = ".$staffInfo['department_id'])->find();
           $staffInfo['department_name'] = $staffDepartment['department_name'];
           unset($staffInfo['department_id']);
           //增加参与时间
           $map['user_id'] = (int)$staffInfo['id'];
           $map['join_time'] = time();
           M('JoinTime')->add($map);
           unset($staffInfo['id']);
        }
        $department = M('department')->select();
        $return = array('code'=>200,'info'=>$staffInfo,'department'=>$department);
        $this->ajaxReturn($return);

    }

    /**
     * 提交调查结果
     */

    public function CommitResult(){
        $result = I('post.result');
        $staff_num = I('post.staff_num/d');
        $staffInfo = $this->checkUserExist($staff_num);
        $return = array('code'=>404,'msg'=>'暂无数据');
        if($result) {
            if (is_array($result)) {
                $investigateResult = M('report');
                foreach ($result as $k => $v) {
                    $report['department_id'] = (int)$v['department_id'];
                    $report['user_id'] = (int)$staffInfo['id'];
                    $report['type'] = (int)$v['type'];
                    $report['comment'] = $v['comment'];
                    $report['advise'] = $v['advise'];
                    $report['add_time'] = time();
                    if (is_array($v['point'])) {
                        $report['participate_view'] =(int) $v['point'][0];
                        $report['unite_statement'] = (int)$v['point'][1];
                        $report['helicopter_thought'] = (int)$v['point'][2];
                        $report['explicit_need'] =(int) $v['point'][3];
                        $report['standard_uniform'] = (int)$v['point'][4];
                        $report['execute_result'] =(int) $v['point'][5];
                        $report['fast_feedback'] =(int) $v['point'][6];
                        $report['rapid_plan'] = (int)$v['point'][7];
                        $report['accomplish_task'] = (int)$v['point'][8];
                        $report['positive_ initiative'] = (int)$v['point'][9];
                        $report['resource_support'] = (int)$v['point'][10];
                        $report['honest_communicate'] =(int) $v['point'][11];
                        $report['civilization_polite'] =(int) $v['point'][12];
                        $report['patient_ considerate'] = (int)$v['point'][13];
                        $report['intime_adjustment'] = (int)$v['point'][14];
                        $report['rapid_adjustment'] =(int) $v['point'][15];
                    }
                    $investigateResult->add($report);
                }
                $return = array('code'=>200,'msg'=>'success!');
                $this->ajaxReturn($return);
            }
        }else{
            $this->ajaxReturn($return);
        }
    }



    /**
     * 检查员工是否存在
     */

    private function checkUserExist($staff_num){
        $Exist = M('UserList')->field('id,staff_name,department_id,staff_num')->where("staff_num = $staff_num")->find();
        if($Exist){
            return $Exist;
        }else{
            $return = array('code'=>404,'msg'=>'该员工不存在！');
        }
        $this->ajaxReturn($return);
    }


}


